# l = [1, 2, 3, 4, 5, 6]
# _m = map(lambda x : x*x, l)
# print(list(_m))

# l = [1, 2, 3, 4, 5, 6]
# f = filter(lambda x: x % 2, l)
# print(list(f))


# def fact(n):
#     print(f"called {n}")
#     if n == 1:
#         return 1
#     return n * fact(n - 1)
#
#
# print(fact(10))


def my_sort(x):
    return x["age"]


def age_add(x):
    x["age"] = x["age"] + 1
    return x


def my_sort_func(my_classes, func_name):
    my_classes.sort(key=func_name)


def my_sort_lamb(my_classes, key):
    my_classes.sort(key=lambda x: x[key])


def fib_recursion(n):
    if n <= 1:
        return n
    return fib_recursion(n-1)+fib_recursion(n-2)


if __name__ == '__main__':
    classes1 = [
        {"name": "abyss", "age": 25, "grade": "A"},
        {"name": "kin", "age": 23, "grade": "B"},
        {"name": "tian", "age": 24, "grade": "B"},
        {"name": "hun", "age": 26, "grade": "A"},
        {"name": "exe", "age": 21, "grade": "A"}
    ]

    classes2 = [
        {"name": "abyss", "age": 25, "grade": "A"},
        {"name": "kin", "age": 23, "grade": "B"},
        {"name": "tian", "age": 24, "grade": "B"},
        {"name": "hun", "age": 26, "grade": "A"},
        {"name": "exe", "age": 21, "grade": "A"}
    ]

    print("将classes按age排序结果如下(使用常规函数):")
    my_sort_func(classes1, my_sort)
    print(classes1)

    print("将classes按age排序结果如下(使用匿名函数):")
    my_sort_lamb(classes2, "age")
    print(classes2)

    print("将classes的age更新后：")
    # _m = map(lambda x: x["age"] + 1, classes1)
    _m = map(age_add, classes1)
    print(list(_m))

    print("classes的grade为A的同学是：")
    f = filter(lambda x: x["grade"] is "A", classes1)
    print(list(f))

    print("使用递归来重构斐波那契函数：")
    for i in range(10):
        print(fib_recursion(i), end=" ")






